<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房屋编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房屋编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="缴费单编号" align="center" prop="payId"/>
      <el-table-column label="缴费金额" align="center" prop="payPrice"/>
      <el-table-column label="缴费日期" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_is_pay" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-coin"
            @click="handlePay(scope.row)"
            v-hasPermi="['domain:pay:handle']"
            v-if="scope.row.status == 0"
          >缴纳租金
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="payDetail(scope.row)"
            v-hasPermi="['domain:pay:query']"
            v-if="scope.row.status == 0"
          >缴费单详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 缴费单详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="form.house.houseId" disabled/>
        </el-form-item>
        <el-form-item label="房屋编号" prop="houseName">
          <el-input v-model="form.house.houseName" disabled/>
        </el-form-item>
        <el-form-item label="缴费人编号" prop="userId">
          <el-input v-model="form.user.userId" disabled/>
        </el-form-item>
        <el-form-item label="缴费人编号" prop="nickName">
          <el-input v-model="form.user.nickName" disabled/>
        </el-form-item>
        <el-form-item label="缴费金额" prop="payPrice">
          <el-input v-model="form.pay.payPrice" disabled/>
        </el-form-item>
        <el-form-item label="缴费日期" prop="payTime">
          <el-date-picker clearable size="small"
                          v-model="form.pay.payTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="缴费状态">
          <el-radio-group v-model="form.pay.status" disabled>
            <el-radio
              v-for="dict in dict.type.house_is_pay"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getPay, listPay, updatePay} from "@/api/system/pay";

export default {
  name: "Pay",
  dicts: ['house_is_pay'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 缴纳租金表格数据
      payList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        payTime: null,
        status: null,
      },
      // 表单参数
      form: {
        house: {},
        user: {},
        pay: {}
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询缴纳租金列表 */
    getList() {
      this.loading = true;
      const url = window.location.pathname;
      const accessPath = url.substr(url.lastIndexOf("/"), url.length);
      //判断当前页面
      switch (accessPath) {
        //中介
        case "/m_payed":
          this.queryParams.status = 1;
          break;
        case "/m_pay":
          this.queryParams.status = 0;
          break;
        //租客
        case "/pay":
          this.queryParams.status = 0;
          break;
        case "/payed":
          this.queryParams.status = 1;
          break;
        default:
          break;
      }
      listPay(this.queryParams).then(response => {
        this.payList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        house: {},
        user: {},
        pay: {}
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.payId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //租客缴纳租金
    handlePay(row) {
      const pay = row;
      this.$modal.confirm("确认要缴纳该房屋的租金吗").then(function () {
        return updatePay(pay);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("缴费成功");
      }).catch(() => {
      });
    },
    //查看缴费单详情
    payDetail(row) {
      getPay(row.payId, row.houseId, row.userId).then(response => {
        this.form.house = response.house;
        this.form.user = response.user;
        this.form.pay = response.pay;
        this.open = true;
        this.title = "缴费单详情"
      });
    }
  }
};
</script>
